package library;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;

import library.IF.BookDAO;

/**
 * The Class Book.
 * 
 * @author Pim Hazebroek
 */
public class Book implements Serializable {
	private static final long serialVersionUID = 1L;

	private static String filename = System.getProperty("user.dir") + "/books.dat";

	private static HashMap<String, Book> books = load();


	/**
	 * Find book.
	 * 
	 * @param title
	 *            the title
	 * 
	 * @return the book
	 */
	static Book findBook(String title) {
		synchronized (books) {
			return books.get(title);
		}
	}

	/**
	 * Removes the book.
	 * 
	 * @param title
	 *            the title
	 */
	static void removeBook(String title) {
		synchronized (books) {
			books.remove(title);
		}
	}

	/**
	 * All books.
	 * 
	 * @return the hash set<book>
	 */
	static HashSet<BookDAO> allBooks() {
		HashSet<BookDAO> booksDAO = new HashSet<BookDAO>();
		synchronized (books) {
			for (Book b: books.values()) {
				BookDAO book = new BookDAO();
				book.setTitle(b.getTitle());
				
				Customer customer = b.getLendTo();
				if (customer != null) {
					book.setLendTo(customer.getName());
				}
				booksDAO.add(book);
			}
		}

		return booksDAO;
	}

	/**
	 * *************************************************************************
	 * \ Object
	 * \****************************************************************
	 * **********.
	 */
	private String title = null;

	/** The lend to. */
	private Customer lendTo = null;

	/**
	 * Instantiates a new book.
	 * 
	 * @param newTitle
	 *            the new title
	 */
	Book(String newTitle) {
		title = newTitle;
		synchronized (books) {
			books.put(title, this);
			save();
		}
	}

	/**
	 * Gets the title.
	 * 
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * Gets the lend to.
	 * 
	 * @return the lend to
	 */
	Customer getLendTo() {
		return lendTo;
	}

	/**
	 * Sets the lend to.
	 * 
	 * @param customer
	 *            the new lend to
	 */
	void setLendTo(Customer customer) {
		lendTo = customer;
	}

	private void save() {
		System.out.println("Book()");
		try {
			System.out.println("Saving books...");
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename));
			oos.writeObject(books);
			oos.close();
			System.out.println("Done saving!");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	private static HashMap<String, Book> load() {
		HashMap<String, Book> books = new HashMap<String, Book>();
		try {
			ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename));
			books = (HashMap<String, Book>) ois.readObject();
			return books;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return books;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "book"; // "[Book: "+title+" "+lendTo.getName()+"]";
	}
}
